import { login, getUserInfo } from '@/api/user'
import { setToken, getToken, clearStorageForDomain } from '@/libs/util'

export default {
	state: {
		userName: '',
		token: getToken()
	},
	mutations: {
		setUserName(state, name) {
			state.userName = name
		},
		setToken(state, token) {
			state.token = token
			setToken(token)
		}
	},
	actions: {
		// 登录
		handleLogin({ commit }, { userName, password }) {
			userName = userName.trim()
			return new Promise((resolve, reject) => {
				login({
					userName,
					password
				})
					.then((res) => {
						const data = res.data
						commit('setToken', data)
						resolve()
					})
					.catch((err) => {
						reject(err)
					})
			})
		},
		// 退出登录
		handleLogOut({ state, commit }) {
			return new Promise((resolve, reject) => {
				// logout(state.token)
				// 	.then(() => {
				// 		commit('setToken', '')
				// 		resolve()
				// 	})
				// 	.catch((err) => {
				// 		reject(err)
				// 	})
				// 如果你的退出登录无需请求接口，则可以直接使用下面三行代码而无需使用logout调用接口
				commit('setToken', null)
				clearStorageForDomain('oidc')
				// commit('setAccess', [])
				resolve()
			})
		},
		// 获取用户相关信息
		getUserInfo({ commit }) {
			return new Promise((resolve, reject) => {
				try {
					getUserInfo()
						.then((res) => {
							const data = res.data
							commit('setUserName', `${data.EmployeeName}(${data.EmployeeNo})`)
							resolve(data)
						})
						.catch((err) => {
							reject(err)
						})
				} catch (error) {
					reject(error)
				}
			})
		}
	}
}
